#!/bin/sh

# script/bootstrap: Resolve all dependencies that the application requires to
#                   run.

set -e

cd "$(dirname "$0")/.."

if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ]; then
  brew update

  brew bundle check 2>&1 >/dev/null || {
    echo "==> Installing Homebrew dependencies…"
    brew bundle
  }
fi

if [ -f ".ruby-version" ] && [ -z "$(rbenv version-name 2>/dev/null)" ]; then
  echo "==> Installing Ruby…"
  rbenv install --skip-existing
  which bundle 2>&1 >/dev/null || {
    gem install bundler
    rbenv rehash
  }
fi

if [ -f "Gemfile" ]; then
  echo "==> Installing gem dependencies…"
  bundle check --path vendor/gems 2>&1 >/dev/null || {
    bundle install --path vendor/gems --quiet --without production
  }

  # Install Git hooks for code quality (development only)
  if [ -f ".lefthook.yml" ] && [ -d ".git" ]; then
    echo "==> Installing Git hooks for code quality…"
    bundle exec lefthook install
  fi
fi
